package com.example.mybatis.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.mybatis.model.ColumnInfo;
import com.example.mybatis.model.SheetInfo;
import com.example.mybatis.model.WorkbookInfo;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ReadWorkbookXmlUtil {

    private static Logger logger = LoggerFactory.getLogger(ReadWorkbookXmlUtil.class);

    public static WorkbookInfo readWorkbookXml(SAXReader saxReader, String pathName){
        //1.检查可想参数是否为空
        if(saxReader==null){
            saxReader = new SAXReader();
        }
         WorkbookInfo workbook = new WorkbookInfo();
        if(pathName==null || "".equals(pathName.trim())){
            throw new NullPointerException("pathName cannot be empty!");
        }
        Map<String, SheetInfo> sheetInfoMap = new HashMap<>();
        File file = new File(pathName);
        try {
            long startTime = System.currentTimeMillis();
            //2.读取xml配置文件转成Document对象
            Document document = saxReader.read(file);
            //3.获取根节点
            Element rootElement = document.getRootElement();
            String workbookName = rootElement.attributeValue("name");
            workbook.setWorkbookName(workbookName);
            //4.获取sheet元素迭代器
            Iterator<Element> sheetIterator = rootElement.elementIterator("sheet");
            while(sheetIterator.hasNext()){
                Element sheetElement = sheetIterator.next();
                String sheetName = sheetElement.attributeValue("name");
                String voClass = sheetElement.attributeValue("voClass");
                SheetInfo sheetInfo = new SheetInfo(sheetName,voClass);
                Iterator<Element> columnIterator = sheetElement.elementIterator("column");
                while(columnIterator.hasNext()){
                    Element columnElement = columnIterator.next();
                    String columnName = columnElement.attributeValue("name");
                    String displayName = columnElement.attributeValue("displayName");
                    String type = columnElement.attributeValue("type");
                    Integer width = columnElement.attributeValue("width")!=null?Integer.valueOf(columnElement.attributeValue("width")):null;
                    ColumnInfo columnInfo = new ColumnInfo(columnName,displayName,type,width);
                    sheetInfo.getColumns().add(columnInfo);
                }
                sheetInfoMap.put(sheetName,sheetInfo);
            }
            workbook.setSheetInfoMap(sheetInfoMap);
            long endTime = System.currentTimeMillis();
            logger.info("dom4j resolve exportExample.xml expense time:"+(endTime-startTime)+"ms");
            logger.info("workbook={}", JSON.toJSONString(workbook, SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullStringAsEmpty));
            return workbook;
        } catch (DocumentException e) {
            throw new RuntimeException("resolve excel file "+pathName+" error:"+e.getMessage());
        }
    }
}
